package com.dmxh.service.impl;

import com.dmxh.mapper.TbGoodsDescMapper;
import com.dmxh.mapper.TbGoodsMapper;
import com.dmxh.pojo.TbGoods;
import com.dmxh.pojo.TbGoodsDesc;
import com.dmxh.service.GoodsDescService;
import com.dmxh.service.GoodsService;
import com.dmxh.vo.GoodsDescVO;
import com.dmxh.vo.GoodsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private TbGoodsMapper tbGoodsMapper;
    @Autowired
    private TbGoodsDescMapper tbGoodsDescMapper;

    @Autowired
    private GoodsDescService goodsDescService;

    @Override
    @Transactional
    public void addInfo(GoodsVo goodsVo) {
        TbGoods tbGoods = new TbGoods();
        TbGoodsDesc tbGoodsDesc = new TbGoodsDesc();
        BeanUtils.copyProperties(goodsVo, tbGoods);
        BeanUtils.copyProperties(goodsVo, tbGoodsDesc);
        tbGoodsMapper.insertForKey(tbGoods);
        tbGoodsDesc.setGoodsId(tbGoods.getId());
        tbGoodsDescMapper.insert(tbGoodsDesc);
    }

    /*显示前端页面主站 详情页查询*/
//    @Override
//    public Map<String, Object> queryGoodsById(Long gid) {
//        TbGoods tbGoods = tbGoodsMapper.selectByPrimaryKey(149187842867946l);
//        GoodsVo goodsVo = new GoodsVo();
//        BeanUtils.copyProperties(tbGoods, goodsVo);
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("goodslist", goodsVo);
//        return map;
//    }


    @Override
    public Map<String, Object> queryGoodsById(Long goodsid) {
        TbGoods tbGoods = tbGoodsMapper.selectByPrimaryKey(goodsid);
        GoodsDescVO goodsDesc = goodsDescService.queryGoodsDescByGoodsId(goodsid);
        GoodsVo goodsVo = new GoodsVo();
        BeanUtils.copyProperties(tbGoods,goodsVo);
        goodsVo.setGoodsDescVO(goodsDesc);
        HashMap<String, Object> map = new HashMap<>();
        map.put("goodslist",goodsVo);
        return map;
    }

    /*商品审核*/
    @Override
    public Map<String, Object> queryAllGoods() {
        List<TbGoods> tbGoods = tbGoodsMapper.selectByExample(null);
        List<GoodsVo> collect = tbGoods.stream().map(goodslist -> {
            GoodsVo goodsVo = new GoodsVo();
            BeanUtils.copyProperties(goodslist, goodsVo);
            return goodsVo;
        }).collect(Collectors.toList());
        HashMap<String, Object> map = new HashMap<>();
        map.put("goodslist", collect);
        return map;
    }

    @Override
    public boolean saveGoods(GoodsVo goodsVo) {
        TbGoods tbGoods = new TbGoods();
        BeanUtils.copyProperties(goodsVo, tbGoods);
        return (tbGoodsMapper.insertSelective(tbGoods) == 1) ? true : false;
    }

    @Override
    public boolean deleteGoods(Long id) {
        return (tbGoodsMapper.deleteByPrimaryKey(id) == 1) ? true : false;
    }

    @Override
    public boolean updateGoods(GoodsVo goodsVo) {
        TbGoods tbGoods = new TbGoods();
        BeanUtils.copyProperties(goodsVo, tbGoods);
        return (tbGoodsMapper.updateByPrimaryKeySelective(tbGoods) == 1) ? true : false;
    }

    /*审核状态*/
    @Override
    public boolean auditStatusGoods(Long goodsId, String status) {
        return (tbGoodsMapper.updateAuditStatus(goodsId, status) == 1) ? true : false;
    }
}
